/*
 * @Author: yan_hai_dong 891236760@qq.com
 * @Date: 2023-03-14 16:10:13
 * @LastEditors: Yhd you@example.com
 * @LastEditTime: 2023-03-14 20:38:10
 * @FilePath: \electron\preload.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const {
    ipcRenderer,
    contextBridge
} = require("electron");
let status = 'end'

window.addEventListener("DOMContentLoaded", () => {
    ipcRenderer.on('up-status', (e, value) => {
        status = value
        if (status == 'start') {
            console.log('开启')
            start();
        } else {
            console.log('关闭')
        }
    })
    ipcRenderer.on('hendle-web', (e, value) => {
        switch (value) {
            case 'follow': {
                getActiveVideo().querySelector('.H2HjqV3h').click();
                break;
            }
            case 'good': {
                getActiveVideo().querySelector('.HNBvVrcV').click();
                break;
            }
            default:
                console.log('不存在的操作')
        }
    })
});

async function start() {
    try {
        let coverImg = getCoverUrl()
        let {
            data,
            code
        } = await ipcRenderer.sendSync('discriminate-img', coverImg)
        if (code == 200) {
            let {
                ImageWidth,
                ImageHeight,
                FaceInfos: [{
                    X,
                    Y,
                    Width,
                    Height,
                    FaceAttributesInfo: {
                        Gender,
                        Age,
                        Expression,
                        Glass,
                        Pitch,
                        Yaw,
                        Roll,
                        Beauty,
                        Hat,
                        Mask,
                        Hair: {
                            Bang,
                            Color,
                            Length,
                        },
                        EyeOpen
                    },
                    FaceQualityInfo: {
                        Score,
                        Sharpness,
                        Brightness,
                        Completeness: {
                            Eyebrow,
                            Eye,
                            Nose,
                            Cheek,
                            Mouth,
                            Chin,
                        }
                    },
                }],
                FaceModelVersion,
            } = data
            console.log('~~option', {
                Gender,
                Beauty,
                Age
            })
            if (Gender < 50 && Beauty >= 60 && Age <= 30) {
                // if (Gender < 50 && Beauty >= 60) {
                pass(coverImg, {
                    Beauty,
                    Age
                });
                return next('这个可以 关注了')
            }
        }
        next()
    } catch (err) {
        console.log(err)
        next()
    }
}

function next(msg = '这个不太行 下一个') {
    console.log(`~~msg:${msg}`)
    if (status == 'end') return;
    setTimeout(() => {
        keydown('down')
        setTimeout(() => {
            start();
        }, 800)
    }, 1500)
}

function pass(coverImg, option) {
    getActiveVideo().querySelector('.H2HjqV3h').click();
    getActiveVideo().querySelector('.HNBvVrcV').click();
    ipcRenderer.send('save-img', coverImg, 'passimg', option)
}

function getActiveVideo() {
    return document.querySelector('[data-e2e="feed-active-video"]')
}

function keydown(key) {
    const keys = {
        down: 40,
        G: 71,
        Z: 90,
    }
    let code = keys[key]
    var e = new KeyboardEvent('keydown', {
        'keyCode': code,
        'which': code
    });
    document.dispatchEvent(e)
}

function getCoverUrl() {
    const coverImg = getActiveVideo().querySelector('.OaNxZqFU img')
    return coverImg.src
}